/**
 * 
 */
package sailing.ui.systemsetting;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.plaf.basic.BasicTableUI;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.AccountingSubject;
import sailing.entity.BankName;
import sailing.entity.SubjectCategory;
import sailing.listener.DataListener;
import sailing.listener.UI;
import sailing.managers.AccountingSubjectManager;
import sailing.managers.BankNameManager;
import sailing.resource.Resource;
import sailing.ui.AbstractJPanel;
import sailing.ui.base.ITabPanel;
import sailing.ui.model.EditBtnCellEditor;
import sailing.ui.model.EditBtnCellRenderer;
import sailing.ui.systemsetting.model.BankNameTableModel;
import sailing.ui.systemsetting.model.SubjectTableModel;
import cjlite.logger.Logger;
import cjlite.utils.Strings;

/**
 * @author kevin
 * 
 */
public class SubjectTablePanel extends AbstractJPanel implements ITabPanel, DataListener {

	private static Logger logger = Logger.thisClass();

	private static ImageIcon folder = Resource.getImageIcon("/ui/tree/tree_folder.png");

	private final SubjectCategory sc;

	private JTable table;
	private SubjectTableModel subjTableModel;

	/**
	 * @param subjectCategory
	 * @param application
	 */
	public SubjectTablePanel(SubjectCategory subjectCategory, SingleFrameApplication application) {
		super(application);
		this.sc = subjectCategory;
		this.initial();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.base.ITabPanel#getTabName()
	 */
	@Override
	public String getTabName() {
		return this.sc.getName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.base.ITabPanel#getContentPanel()
	 */
	@Override
	public JPanel getContentPanel() {
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.base.ITabPanel#getTabIcon()
	 */
	@Override
	public Icon getTabIcon() {
		return folder;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.base.ITabPanel#getTabTip()
	 */
	@Override
	public String getTabTip() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.AbstractJPanel#initialPanel()
	 */
	@Override
	protected void initialPanel() {
	}

	protected void initial() {
		JPanel root = new JPanel();
		root.setLayout(new BorderLayout());
		root.add(this.createTop(), BorderLayout.NORTH);

		root.add(this.createTable(), BorderLayout.CENTER);

		this.setLayout(new BorderLayout());
		this.add(root, BorderLayout.CENTER);
		this.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
	}

	private Component createTable() {
		subjTableModel = new SubjectTableModel(this.sc, this.getApplication());
		table = new JTable();
		table.setUI(new BasicTableUI());

		table.setModel(subjTableModel);
		table.setRowHeight(30);

		subjTableModel.updateModel();

		JScrollPane spane = new JScrollPane(table);
		spane.setBackground(new Color(100, 100, 100, 0));
		return spane;
	}

	private Component createTop() {
		Box box = Box.createHorizontalBox();
		box.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
		box.add(Box.createHorizontalStrut(10));
		box.add(new JLabel(this.sc.toString()));
		box.add(Box.createHorizontalGlue());
		return box;
	}

	public void showHideEditBtn(boolean selected) {
		this.subjTableModel.showEditBtn(selected);
		// this.customerTableModel.fireTableDataChanged();
		this.subjTableModel.fireTableStructureChanged();
		if (selected) {
			this.table.getColumnModel().getColumn(2).setCellRenderer(new EditBtnCellRenderer("编辑", "删除"));
			EditBtnCellEditor editBtnCell = new EditBtnCellEditor("编辑", "删除");
			editBtnCell.addBtn1Listener(editAL);
			editBtnCell.addBtn2Listener(deleteAL);
			this.table.getColumnModel().getColumn(2).setCellEditor(editBtnCell);
		}
	}

	ActionListener editAL = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() instanceof EditBtnCellEditor) {
				EditBtnCellEditor ce = (EditBtnCellEditor) e.getSource();
				if (ce.getCellEditorValue() instanceof AccountingSubject) {

					AccountingSubject as = (AccountingSubject) ce.getCellEditorValue();
					logger.trace("edit AccountingSubject '{0}'", as.toString());
					NewAccountingSubjectDialog nasd = new NewAccountingSubjectDialog(getApplication().getMainFrame(),
							getApplication());

					nasd.setPreSubject(as);
					nasd.setPreSubjectCategory(sc);

					nasd.setVisible(true);
				}
			}
		}
	};

	ActionListener deleteAL = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() instanceof EditBtnCellEditor) {
				EditBtnCellEditor ce = (EditBtnCellEditor) e.getSource();
				if (ce.getCellEditorValue() instanceof AccountingSubject) {
					AccountingSubject as = (AccountingSubject) ce.getCellEditorValue();
					logger.trace("delete AccountingSubject '{0}'", as.toString());
					int result = JOptionPane.showConfirmDialog(SubjectTablePanel.this,
							Strings.concat("确定要删除：'", as.getName(), "' ?"), "提示", JOptionPane.YES_NO_OPTION);
					if (JOptionPane.YES_OPTION == result) {
						AccountingSubjectManager asm = SubjectTablePanel.this.getApplication().getContext().getIoc()
								.getInstance(AccountingSubjectManager.class);

						asm.delete(as);
						UI.refresh(SubjectTablePanel.class);
					}
				}
			}
		}
	};

	@Override
	public void dataChanged() {
		this.subjTableModel.updateModel();
	}

	public SubjectCategory getSubjectCategory() {
		return this.sc;
	}

}
